import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;

public class ConfigPanel extends JPanel {

	/**
	 * This is the panel in the Config Frame that holds the options for configuring the game.
	 * 
	 * @author "It's A Trap!!"
	 * @version 5.1 10/10/2011
	 */

	private JTextField playerNameTxt;
	private JTextField[] settlerTextFields=new JTextField[4];

	private JRadioButton[] professions=new JRadioButton[3];
	private JRadioButton[] rations=new JRadioButton[3];
	private JRadioButton[] paces=new JRadioButton[3];

	private ButtonGroup profButton;
	private ButtonGroup rationButton;
	private ButtonGroup paceButton;
	private JButton btnContinue;
	static final long serialVersionUID = 0L;

	/**
	 * Create the panel.
	 */

	public ConfigPanel() {

		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(new MigLayout("", "[50.00][75.00][93.00,grow][142.00][84.00][60.00][93.00,grow]", "[][][29.00,grow][grow][27.00,grow][22.00][27.00][33.00][29.00][25.00]"));

		//"Assemble your Party"
		JLabel lblAssembleYourParty = new JLabel("Assemble Your Party");
		add(lblAssembleYourParty, "cell 3 0");

		//"Name:"
		JLabel lblName = new JLabel("Name:");
		add(lblName, "cell 1 2,alignx right");

		//"Name text field"
		playerNameTxt = new JTextField();
		add(playerNameTxt, "cell 2 2,growx");
		playerNameTxt.setColumns(10);

		//"Person 1:"
		JLabel lblPassenger = new JLabel("Settler 1:");
		add(lblPassenger, "cell 5 2,alignx trailing");

		//Person 1 text field
		settlerTextFields[0] = new JTextField();
		add(settlerTextFields[0], "cell 6 2,growx");
		settlerTextFields[0].setColumns(10);

		//Profession label
		JLabel lblProfession = new JLabel("Profession:");
		add(lblProfession, "cell 1 3,alignx right,aligny top");

		//Profession radio button group
		profButton = new ButtonGroup();
		professions[0] = new JRadioButton("Farmer");
		professions[1] = new JRadioButton("Carpenter");
		professions[2] = new JRadioButton("Banker");

		profButton.add(professions[0]);
		profButton.add(professions[1]);
		profButton.add(professions[2]);

		professions[0].setSelected(true);

		add(professions[0], "cell 2 3,aligny top");
		add(professions[1], "cell 2 4,aligny top");
		add(professions[2], "cell 2 5");

		//Farmer description
		JTextPane txtpnFarmersCanForage = new JTextPane();
		txtpnFarmersCanForage.setText("Can forage for food on trail ($400)");
		txtpnFarmersCanForage.setEditable(false);
		add(txtpnFarmersCanForage, "cell 3 3");

		//"Person 2:"
		JLabel lblPassenger_1 = new JLabel("Settler 2:");
		add(lblPassenger_1, "cell 5 3,alignx trailing");

		//Person 2 text field
		settlerTextFields[1] = new JTextField();
		add(settlerTextFields[1], "cell 6 3,growx");
		settlerTextFields[1].setColumns(10);

		//Carpenter description
		JTextPane txtpnCarpentersCanFix = new JTextPane();
		txtpnCarpentersCanFix.setText("Can fix wagon, less repairs ($800)");
		txtpnCarpentersCanFix.setEditable(false);
		add(txtpnCarpentersCanFix, "cell 3 4");

		//"Person 3:"
		JLabel lblPassenger_2 = new JLabel("Settler 3:");
		add(lblPassenger_2, "cell 5 4,alignx trailing");

		//Person 3 text field
		settlerTextFields[2] = new JTextField();
		add(settlerTextFields[2], "cell 6 4,growx");
		settlerTextFields[2].setColumns(10);

		//Banker description
		JTextPane txtpnBankersStartOff = new JTextPane();
		txtpnBankersStartOff.setText("Bankers start off with more money ($1600)");
		txtpnBankersStartOff.setEditable(false);
		add(txtpnBankersStartOff, "cell 3 5");

		//"Person 4:"
		JLabel lblPassenger_3 = new JLabel("Settler 4:");
		add(lblPassenger_3, "cell 5 5,alignx trailing");

		//Person 4 text field
		settlerTextFields[3] = new JTextField();
		add(settlerTextFields[3], "cell 6 5,growx");
		settlerTextFields[3].setColumns(10);

		//"Rations:"
		JLabel lblRations = new JLabel("Rations:");
		add(lblRations, "cell 1 7,alignx right");

		//Rations radio button group
		rationButton = new ButtonGroup();
		rations[0] = new JRadioButton("Filling");
		rations[1] = new JRadioButton("Meager");
		rations[2] = new JRadioButton("Bare Bones");

		rationButton.add(rations[0]);
		rationButton.add(rations[1]);
		rationButton.add(rations[2]);

		rations[0].setSelected(true);

		add(rations[0], "cell 2 7,alignx left");
		add(rations[1], "cell 3 7,alignx center");
		add(rations[2], "cell 4 7");

		//Ok button	
		JButton btnOk = new JButton("Create");
		btnOk.addActionListener(new ButtonListener());
		
		btnContinue = new JButton("Continue");
		btnContinue.setEnabled(false);
		add(btnContinue, "cell 5 9");
		add(btnOk, "cell 6 9,alignx center");

		//"Pace:"
		JLabel lblPace = new JLabel("Pace:");
		add(lblPace, "cell 1 8,alignx right");

		//pace radio button group
		paceButton = new ButtonGroup();

		paces[0] = new JRadioButton("Steady");
		paces[1] = new JRadioButton("Strenuous");
		paces[2] = new JRadioButton("Grueling");


		paceButton.add(paces[0]);
		paceButton.add(paces[1]);
		paceButton.add(paces[2]);

		add(paces[0], "cell 2 8");
		add(paces[1], "cell 3 8,alignx center");
		add(paces[2], "cell 4 8");

		paces[0].setSelected(true);

	}

	/**
	 * Listens for when the "ok" button is pressed.
	 */
	public JButton getContinueButton(){
		return btnContinue;
	}

	private class ButtonListener implements ActionListener{

		/**
		 * Configures the game if all text fields are satisfied and displays an alert when these requirements aren't met
		 * 
		 * @param okPressed Event for when the "ok" button is pressed
		 */

		public void actionPerformed(ActionEvent okPressed) {


			if((playerNameTxt.getText().trim().equals("")) || (settlerTextFields[0].getText().trim().equals("")) || (settlerTextFields[1].getText().trim().equals("")) || (settlerTextFields[2].getText().trim().equals("")) || (settlerTextFields[3].getText().trim().equals(""))){
				JOptionPane.showMessageDialog(null, "You have not named all characters. Please give a name to every settler.");
			}
			else{
				String ration="";
				Pace pace;
				String[] settlerNames=new String[4];
				String playerName=playerNameTxt.getText();
				Profession profession;

				//Get Ration
				for (JRadioButton rationBtn:rations){
					if (rationBtn.isSelected()){
						ration=rationBtn.getText();
						break;
					}
					
				}				
				//Get Pace
				String selPace="";
				for (JRadioButton paceBtn:paces){
					if (paceBtn.isSelected()){
						selPace=paceBtn.getText();
						break;
					}
					
				}
				if (selPace.equals("Steady"))
					pace=Pace.Steady;
				else if (selPace.equals("Grueling"))
					pace=Pace.Grueling;
				else
					pace=Pace.Strenuous;

				//Get Settler Names
				for (int i=0; i<settlerNames.length; i++){
					settlerNames[i]=settlerTextFields[i].getText();
				}

				//Get Profession
				String selProf="";
				for (JRadioButton profBtn:paces){
					if (profBtn.isSelected()){
						selPace=profBtn.getText();
						break;
					}
					
				}
				if (selProf.equals("Farmer"))
					profession=Profession.FARMER;
				else if (selProf.equals("Carpenter"))
					profession=Profession.CARPENTER;
				else
					profession=Profession.BANKER;

				WagonController wControl = new WagonController(ration,settlerNames,pace,playerName, profession);
				System.out.println(wControl.getWagon().toString());
				btnContinue.setEnabled(true);
			}


		}



	}

}
